package com.hotau.account;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.hotau.Executable;
import com.hotau.HotauActivity;
import com.hotau.HotauConstants;
import com.hotau.R;
import com.hotau.member.service.MemberService;
import com.hotau.util.DialogUtil;
import com.hotau.util.HttpRequestAsynTask;
import com.hotau.util.HttpUtil;
import com.hotau.util.ValidateUtils;

/**
 * 修改密码
 * @author liujia (liujia9234@126.com)
 * @data @2011-12-10
 * @version 1.0
 * <ul>
 * 		<li>1.0 创建</li>
 * </ul>
 */
public class PasswordEditFragment extends Fragment implements OnClickListener {
	
	public static final String TAG = "PasswordEditFragment";
	private EditText password_et_odl_pwd = null;
	private EditText password_et_new_pwd = null;
	private EditText password_et_verifynew_pwd = null;
	private CheckBox password_cb_pwddis = null;
	private Button password_btn_submit = null;
	private boolean isPayPwd = false;
	
	/**
	 * 异步请求Task
	 */
	HttpRequestAsynTask password_edit_loginpwd_task = null; //修改登录密码
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initAllWidget();
		initAllEvent();
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		isPayPwd = getArguments().getBoolean("isPayPwd");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.account_password_edit_pwd, container,false);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.account_password_cb_pwddis:
			if(password_cb_pwddis.isChecked()){
				password_et_odl_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				password_et_new_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				password_et_verifynew_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
			}else{
				password_et_odl_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
				password_et_new_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
				password_et_verifynew_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
			}
			break;
		case R.id.account_password_btn_submit:
			if(password_et_odl_pwd.getText().length() < 8 || password_et_odl_pwd.getText().length() >20){
				DialogUtil.showDialog(getActivity(), "", "旧密码长度必须8-20位");
				return;
			}
			if(password_et_new_pwd.getText().length() < 8 || password_et_new_pwd.getText().length() >20){
				DialogUtil.showDialog(getActivity(), "", "新密码长度必须8-20位");
				return;
			}
			if(password_et_verifynew_pwd.getText().length() < 8 || password_et_verifynew_pwd.getText().length() >20){
				DialogUtil.showDialog(getActivity(), "", "确认新密码长度必须8-20位");
				return;
			}
			
			if( !password_et_new_pwd.getText().toString().equals(password_et_verifynew_pwd.getText().toString())){
				DialogUtil.showDialog(getActivity(), "", "两次输入密码不一致");
				return;
			}
			
			if(isPayPwd)
			{
			    if (!ValidateUtils.isLetteric(password_et_new_pwd.getText().toString().trim().substring(0, 1)))
	            {
	                DialogUtil.showDialog(getActivity(), "", "密码第一位必须为字母");
	                return;
	            }
			}
			
	        if (!ValidateUtils.isNumberLetteric(password_et_new_pwd.getText().toString().trim()))
	        {
	            DialogUtil.showDialog(getActivity(), "", "密码必须为字母和（或）数字的组合");
	            return;
	        }
			
			
			
			StringBuilder sb_edit_pwd = new StringBuilder();
			sb_edit_pwd.append("Form:token="+MemberService.getToken(getActivity()));
			sb_edit_pwd.append(";type="+(isPayPwd?"2":"1"));
			sb_edit_pwd.append(";oldPwd="+password_et_odl_pwd.getText());
			sb_edit_pwd.append(";newPwd="+password_et_new_pwd.getText());
			sb_edit_pwd.append(";confirmPwd="+password_et_verifynew_pwd.getText());
			sb_edit_pwd.append(";terminalCode=" + HotauConstants.getTerminalCode(getActivity()));
			password_edit_loginpwd_task = new HttpRequestAsynTask(getActivity()){
				HotauActivity csmbpActivity = (HotauActivity) getActivity();
				@Override
				protected void doPostExecute(String result) {
					// TODO Auto-generated method stub
					Log.d(TAG, "password_edi_response :" + result);
					try {
						JSONObject editpwd_result = new JSONObject(result);
						if(editpwd_result != null && editpwd_result.getInt("result") == 1){
							//修改成功
//							csmbpActivity.pushFragment("信息提示", new MessagePromptFragment(true,"修改登录密码成功"));
							String success = null;
							if(isPayPwd)
							{
							    success =  "修改支付密码成功";
							}
							else
							{
							    success =  "修改登录密码成功";
							}
						    DialogUtil.showDialog(csmbpActivity, "", success,new Executable() {
								
								@Override
								public void execute() {
									// TODO Auto-generated method stub
									csmbpActivity.popFragment();
								}
							});
						}else{
							//修改失败
//							csmbpActivity.pushFragment("信息提示", new MessagePromptFragment(false,editpwd_result.getString("errorMsg")));
							DialogUtil.showDialog(csmbpActivity, "", editpwd_result.getString("errorMsg"));
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						DialogUtil.showDialog(getActivity(), "", "登录返回数据出错 ");
					}
				}
			};
			password_edit_loginpwd_task.execute(HotauConstants.ACCOUNT_EDIT_PWD_URL,sb_edit_pwd.toString(),HttpUtil.UTF8_ENCODING);
			break;

		default:
			break;
		}
	}
	
	/**
	 * 初始化所有控件
	 */
	public void initAllWidget(){
		password_et_odl_pwd = (EditText) getActivity().findViewById(R.id.account_password_et_odl_pwd);
		password_et_new_pwd = (EditText) getActivity().findViewById(R.id.account_password_et_new_pwd);
		password_et_verifynew_pwd = (EditText) getActivity().findViewById(R.id.account_password_et_verifynew_pwd);
		password_cb_pwddis = (CheckBox) getActivity().findViewById(R.id.account_password_cb_pwddis);
		password_btn_submit = (Button) getActivity().findViewById(R.id.account_password_btn_submit);
		
		if(isPayPwd){
			TextView password_tv_odl_pwd = (TextView) getActivity().findViewById(R.id.account_password_tv_odl_pwd);
			password_tv_odl_pwd.setText("旧支付密码：");
			TextView password_tv_new_pwd = (TextView) getActivity().findViewById(R.id.account_password_tv_new_pwd);
			password_tv_new_pwd.setText("输入新支付密码：");
			TextView password_tv_verifynew_pwd = (TextView) getActivity().findViewById(R.id.account_password_tv_verifynew_pwd);
			password_tv_verifynew_pwd.setText("确认新支付密码：");
		}
		
	}
	
	/**
	 * 初始化所有事件
	 */
	public void initAllEvent() {
		password_cb_pwddis.setOnClickListener(this);
		password_btn_submit.setOnClickListener(this);
	}
	

}
